package cn.mst.vrs.controller.admin.payrefund;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.mst.vrs.controller.admin.payrefund.vo.PayRefundPageReqVO;
import cn.mst.vrs.controller.admin.payrefund.vo.PayRefundRespVO;
import cn.mst.vrs.controller.admin.payrefund.vo.PayRefundSaveReqVO;
import cn.mst.vrs.dal.dataobject.payrefund.PayRefundDO;
import cn.mst.vrs.service.payrefund.PayRefundService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 支付退款单")
@RestController
@RequestMapping("/vrs/pay-refund")
@Validated
public class PayRefundController {

    @Resource
    private PayRefundService payRefundService;

    @PostMapping("/create")
    @Operation(summary = "创建支付退款单")
    @PreAuthorize("@ss.hasPermission('vrs:pay-refund:create')")
    public CommonResult<Long> createPayRefund(@Valid @RequestBody PayRefundSaveReqVO createReqVO) {
        return success(payRefundService.createPayRefund(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新支付退款单")
    @PreAuthorize("@ss.hasPermission('vrs:pay-refund:update')")
    public CommonResult<Boolean> updatePayRefund(@Valid @RequestBody PayRefundSaveReqVO updateReqVO) {
        payRefundService.updatePayRefund(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除支付退款单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vrs:pay-refund:delete')")
    public CommonResult<Boolean> deletePayRefund(@RequestParam("id") Long id) {
        payRefundService.deletePayRefund(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得支付退款单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vrs:pay-refund:query')")
    public CommonResult<PayRefundRespVO> getPayRefund(@RequestParam("id") Long id) {
        PayRefundDO payRefund = payRefundService.getPayRefund(id);
        return success(BeanUtils.toBean(payRefund, PayRefundRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得支付退款单分页")
    @PreAuthorize("@ss.hasPermission('vrs:pay-refund:query')")
    public CommonResult<PageResult<PayRefundRespVO>> getPayRefundPage(@Valid PayRefundPageReqVO pageReqVO) {
        PageResult<PayRefundDO> pageResult = payRefundService.getPayRefundPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PayRefundRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出支付退款单 Excel")
    @PreAuthorize("@ss.hasPermission('vrs:pay-refund:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPayRefundExcel(@Valid PayRefundPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PayRefundDO> list = payRefundService.getPayRefundPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "支付退款单.xls", "数据", PayRefundRespVO.class,
                BeanUtils.toBean(list, PayRefundRespVO.class));
    }

}